<?php
$logger->debug('$_SESSION:');
$logger->debug($_SESSION);
$logger->debug('$_GET:');
$logger->debug($_GET);
$logger->debug('$skin:');
$logger->debug($skin);
/* 'destination' => 'movie',
  'page' => 'wall',
  'key' => '5',
  'category' => 'all',
  'index' => '1' */
$_SESSION['key'] = $_GET['key'];
$_SESSION['category'] = $_GET['category'];
$_SESSION['filter'] = $_GET['filter'];
if (isset($_GET['filter'])) {
    $xmlMedia = plexGetMedia($_GET['key'], $_GET['category'], $_GET['filter']);
} else {
    $xmlMedia = plexGetMedia($_GET['key'], $_GET['category']);
}

//$filters = plexGetMovieFilters($_GET['key']);
//$logger->debug($filters);


//$videosNodes = $xmlMedia->xpath('//Video');
//$numVideos = count($videosNodes);
$numVideos = $xmlMedia['size'];
$logger->debug('$numVideos: ' . $numVideos);
$lastIndex = ceil($numVideos / ($skin['columns'] * $skin['rows']));

$thumbIcons = $homePath . '/images/thumbs/movies/';
$thumbOverlay = $homePath . '/images/movies/wall/overlays/';
?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" type="text/css" media="screen" href="<?php echo $homePath . "/css/wall.css.php" ?>" />
        <script type="text/javascript" src="<?php echo $homePath . "/js/movie_wall.js.php" ?>" ></script>
    </head>
    <body>

        <a href="" tvid="#" id="play" vod="" />
        <table border="0" cellpadding="0" cellspacing="0" align="left">
            <tr valign="top">
                <td height="598">
                    <table class="main" border="0" cellpadding="0" cellspacing="0">
                        <tr valign="top">
                            <td>
                                <table class="main" border="0" cellpadding="0" cellspacing="0">
                                    <tr valign="top">
                                        <td align="left" valign="top" height="56" width="1">
                                            <img src="<?php echo $homePath . '/images/movies/wall/html' ?>/1x688.png" height="56"/>
                                        </td>
                                        <td id="indexmenuleft" width="265" valign="top" height="56" align="left">
                                            <table class="main" border="0" cellpadding="0" cellspacing="0">
                                                <tr>
                                                    <td class="indexname">
                                                        <?php
                                                            if (empty($xmlMedia['title2'])) {
                                                                echo $xmlMedia['title1'] . '->All';
                                                            } else {
                                                                switch ($_GET['category']){
                                                                    case 'actor':
                                                                        echo $xmlMedia['title1'] . '->By Starring Actor->' . $xmlMedia['title2'];    
                                                                        break;
                                                                    case 'director':
                                                                        echo $xmlMedia['title1'] . '->By Director->' . $xmlMedia['title2'];    
                                                                        break;
                                                                    case 'year':
                                                                        echo $xmlMedia['title1'] . '->By Year->' . $xmlMedia['title2'];    
                                                                        break;
                                                                    default :
                                                                        echo $xmlMedia['title1'] . '->' . $xmlMedia['title2'];
                                                                        break;
                                                                }
                                                            }
                                                        ?>
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                        <td id="indexmenuright" align="right">
                                            <table cellspacing="0" cellpadding="0" border="0" background="<?php echo $homePath . '/images/movies/wall/html' ?>/filterbg1.png">
                                                <tbody>
                                                    <tr>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr align="left" valign="top">
                            <td>
                                <table class="movies" border="0" cellpadding="0" cellspacing="4" align="left">
                                    <tbody>
                                        <?php
                                        $beginning = (int) ($_GET['index'] * $skin['columns'] * $skin['rows'] - $skin['columns'] * (int) $skin['rows'] + 1);
                                        $end = (int) ($beginning + $skin['columns'] * $skin['rows'] - 1);
                                        $count = 1;
                                        $row = 1;
                                        $wallIndex = 1;
                                        $logger->info('$beginning: ' . $beginning . ', $end: ' . $end);

                                        if ($_GET['index'] == 1) {
                                            $previousIndex = $lastIndex;
                                        } else {
                                            $previousIndex = ($_GET['index'] - 1);
                                        }

                                        if ($_GET['index'] == $lastIndex) {
                                            $nextIndex = 1;
                                        } else {
                                            $nextIndex = $_GET['index'] + 1;
                                        }

                                        foreach ($xmlMedia->Video as $video) {
                                            if ($count >= $beginning AND $count <= $end) {
                                                //$logger->info('$count: ' . $count . ', $beginning: ' . $beginning . ', $end: ' . $end . ', $count % $skin[\'columns\']): ' . ($count % $skin['columns']));
                                                if ($count == $beginning) {
                                                    $currentRow = 'top';
                                                    echo '<tr id=\"top\">';
                                                }
                                                // Set up down navigation.
                                                switch ($currentRow) {
                                                    case 'top':
                                                        $onkeyup = 'ONKEYUPSET="pgup"';
                                                        $onkeydown = 'ONKEYDOWNSET="' . ($wallIndex + $skin['columns']) . '"';
                                                        break;
                                                    case 'middle':
                                                        $onkeyup = 'ONKEYUPSET="' . $wallIndex - $skin['columns'] . '"';
                                                        $onkeydown = 'ONKEYDOWNSET="' . ($wallIndex + $skin['columns']) . '"';
                                                        break;
                                                    case 'bottom':
                                                        $onkeyup = 'ONKEYUPSET="' . $wallIndex - $skin['columns'] . '"';
                                                        $onkeydown = 'ONKEYDOWNSET="pgdn"';
                                                        break;
                                                    default:
                                                        break;
                                                }

                                                // Set left right navigation.
                                                switch ($count % $skin['columns']) {
                                                    case 0:
                                                        $onkeyleft = 'ONKEYLEFTSET="' . ($wallIndex - 1) . '"';
                                                        $onkeyright = 'ONKEYRIGHTSET="' . ($wallIndex - $skin['columns'] + 1) . '"';
                                                        break;
                                                    case 1:
                                                        $onkeyleft = 'ONKEYLEFTSET="' . ($wallIndex + $skin['columns'] - 1) . '"';
                                                        $onkeyright = 'ONKEYRIGHTSET="' . ($wallIndex + 1) . '"';
                                                        break;
                                                    default:
                                                        $onkeyleft = 'ONKEYLEFTSET="' . ($wallIndex - 1) . '"';
                                                        $onkeyright = 'ONKEYRIGHTSET="' . ($wallIndex + 1) . '"';
                                                        break;
                                                }

                                                
                                                ////http://192.168.0.3:32400/library/parts/14807/Afro%20Samurai%20-%20Resurrection%20(2009).avi
                                                // plex change: now http://192.168.0.3:32400/library/parts/14807/file.avi
                                                $playPath = $config['plex_url'] . urldecode($video->Media->Part['key']);
                                                //$logger->info('Play Path: ' . $playPath);
                                            
                                                $videoOf = $count . '/' . $numVideos . ' ' . $xmlMedia['title1'];

                                                echo '	<td align="center" background="' . plexGetImage($video['thumb'], 117, 174, 'thumbs/movies/') . '"> ';
                                                echo '      <a href="?destination=' . $_GET['destination'] . '&page=details&key=' . $video['key'] . '" name="' . $wallIndex . '" ' . $onkeyup . ' ' . $onkeydown . ' ' . $onkeyleft . ' ' . $onkeyright . ' onfocus="set(\'' . addslashes($playPath) . '\', \'' . $videoOf . '\', ' . $wallIndex . ');" onblur="unset();" onmouseover="set(\'' . addslashes($playPath) . '\', \'' . $videoOf . '\', ' . $wallIndex . ');" onmouseout="unset();"/>';
                                                echo '          <img width="117" height="174" src="' . $thumbOverlay . 'posteroverlay2.png" onfocussrc="' . $thumbOverlay . 'glassoverlay5.png" />';
                                                echo '      </a>';
                                                echo '  </td>';

                                                if ($count == ($end - $skin['columns'])) {
                                                    $currentRow = 'bottom';
                                                    echo '</tr>';
                                                    echo '<tr id=\"bottom\">';
                                                }

                                                if ($count % $skin['columns'] == 0 AND $currentRow != 'bottom') {
                                                    $currentRow = 'middle';
                                                    $onkeyup = 'ONKEYUPSET="' . $wallIndex - $skin['columns'] . '"';
                                                    echo '</tr>';
                                                    echo '<tr id=\"middle\">';
                                                }
                                                $wallIndex++;
                                            }
                                            $count++;
                                        }
                                        echo '</tr>';
                                        ?>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <table border="0" cellpadding="10" cellspacing="0" width="100%" align="center">
            <tr>
                <td width="25%" align="left" id="number" valign="top">xxx</td>
                <td width="50%" align="center" id="title" valign="top">bob</td>
                <td width="25%" align="right" id="page" valign="top">Page <?php echo $_GET['index'] . ' / ' . $lastIndex; ?></td>
            </tr>
            <tr>
                <td></td>
                <td align="right" id="year" valign="top"></td>
            </tr>
        </table>
        <div id="cat1" class="popup">
            <span id="catSpan1">
                
            </span>
        </div>
        <div id="cat2" class="popup">
            <span id="catSpan2">
                
            </span>
        </div>
        <div id="cat3" class="popup">
            <span id="catSpan3">
                
            </span>
        </div>
        <div id="cat4" class="popup">
            <span id="catSpan4">
                
            </span>
        </div>
        <div id="cat5" class="popup">
            <span id="catSpan5">
                
            </span>
        </div>
            <?php
            $titleCount = 1;
            $count = 1;
            foreach ($xmlMedia->Video as $video) {
                if ($count >= $beginning AND $count <= $end) {
                    $playPath = $config['plex_url'] . $video->Media->Part['key'];
                    echo '<div id="title' . $titleCount . '" class="title"  align="center">';
                    echo $video['title'];
                    echo '</div>';
                    $titleCount++;
                }
                $count++;
            }
            ?>
        <div class="control">
            <a href="<?php echo '?destination=' . $_GET['destination'] . '&page=wall&key=' . $_GET['key'] . '&category=all&index=' . $previousIndex ?>" name="pgup" ONFOCUSLOAD=""></a>
            <a href="<?php echo '?destination=' . $_GET['destination'] . '&page=wall&key=' . $_GET['key'] . '&category=all&index=' . $nextIndex ?>" name="pgdn" ONFOCUSLOAD=""></a>
            <a href="<?php echo '?destination=home' ?>" tvid="HOME" ONFOCUSLOAD=""></a>
        </div>
    </body>
</html>
<?php
unset($xmlMedia);
unset($numVideos);
unset($beginning);
?>
